	jr Boot
  	db 0,0,0,0,0,0
  	
	jp rmacro		; rst 08h
	db 0,0,0,0,0
	
	jp FastCopy		; rst 10h
	db 0,0,0,0,0
	
	jp DrawWrappedString	; rst 18h
 	db 0,0,0,0,0
 	
	jp EndThread		; rst 20h
	db 0,0,0,0,0
	
	db 0,0,0		; rst 28h
	db 0,0,0,0,0
	
	db 0,0,0		; rst 30h
	db 0,0,0,0,0
	
	jp SysInterrupt		; rst 38h
 	dw 0,0,0,0,0,0
	dw 0,0,0,0,0,0
	
	jp Boot
	dw 0A5FFh
Boot:	; 0058h
	di
	ld sp,RAMEnd-2		; Set the stack
	; Initialize memory
	ld a, 00000110b
	out (4), a
#ifdef TI83P
#else
	ld a, 80h
	out (5), a
#endif
#ifdef TI83P
	ld a, 41h
#else
	ld a, 81h
#endif
	out (7), a
	; Wait for ON to be pressed in a low power state
#ifdef SLEEP_ON_FIRST_BOOT
	; if a reboot was called then we should disable the Panel and Multitasking
	sto(DISABLED,OverlayEnabled)
	call Sleep
#endif
#ifndef TI83P
	; set cpu to 15MHz mode
	in a,01h
	out (20h), a
#endif
	; Clear Ram
;	ld hl,RAMStart
;	ld (hl),0
;	ld de,RAMStart+1
;	ld bc,RAMEnd-RAMStart-1
;	ldir
	ld hl,$8000 \ ld de,$8001 \ ld b,h \ ld c,l \ ld (hl),l \ ldir
	; Make sure thta multitasking is disabled and turn of the panel display as well as disable the overlay
	sto(DISABLED,MultitaskEnabled)
	ld (PanelEnabled),a
	ld (OverlayEnabled),a
	; Setup the RAM FS
	ld bc,UserRAMEnd-UserRAM-2
	ld a,0FFh
	ld hl,UserRAM+2
	call AllocSection
	; Setup The Keyboard
	ld a, 0FFh
	out (1),a
	; Initialize LCD
	ld a, 40h
	out (10h), a
	ld a, 05h
	call LCDDelay
	out (10h), a
	ld a, 01h
	call LCDDelay
	out (10h), a
	ld a, 3
	call LCDDelay
	out (10h), a
	ld a, 0F6h
	call LCDDelay
	out (10h), a
	; Boot Code
	ld a,-1
	ld (LCDLock),a	; LCD is not locked
#ifdef DEBUG
	call ResetTextLocation
#endif
	ld de,FileToLoad
	call NewThread
	if(SUCCESS)
		jr nz,KernelPanic
#ifdef DEBUG
	call LoadReturnText	
	call DrawWrappedString
	call FastCopy
	call Newline
#endif
	ld a,-1
	ld (CurrentThreadID),a	; set up the current thread as the system thread
	ld a,ENABLED
	ld (MultitaskEnabled),a	; enable multitasking
	ei	; enable interrupts
	halt	; wait for interrupt to happen in low power mode
KernelPanic:
	di	; disable interrupts, after all the system is shutting down
	push af	; store the error code
		sto(DISABLED,PanelEnabled)	; turn off the panel, lets not waste cycles with this
#ifndef DEBUG
		call ResetTextLocation
		call ClearBuffer
#endif
		ld hl,KPMsg		; get the kernel panic message
		call DrawWrappedString	; draw it
	pop af			; recall the error code
	call LoadReturnText	; get the error message
	call DrawWrappedString	; draw it
	call FastCopy		; display it
	call FlushKeys		; make sure nothing is pressed
	call WaitKey		; wait for a keypress
	jp Boot			; then reboot
FileToLoad:
	db "boot/boot.bin",0

KPMsg:
	db "Kernel Panic\n",0
#ifdef DEBUG
AddressMsg:
	db "Address:",0
ErrorCodeMsg:
	db "Return Code:",0
TypeIDMsg:
	db "Type ID:",0
NameMsg:
	db "File Name:",0
NameDirMsg:
	db "Dir Name:",0
NameSearchMsg:
	db "Searching For:",0
PageMsg:
	db "ROM Page:",0
#endif
